;(function() {
	
	// Constants defining the indexes of each view
	// (as they are added to the view manager)
	
	Nokia.VIEW_HOME = 0;
	Nokia.VIEW_RESULTS = 1;
	Nokia.VIEW_WEATHER = 2;
	Nokia.VIEW_TWITTER = 3;
	Nokia.VIEW_PICASA = 4;
	Nokia.VIEW_NEWS = 5;
	Nokia.VIEW_WIKIPEDIA = 6;
	
	Nokia.GBC = Nokia.Widget.extend({
		
		render: function() {
			var instance = this;
			
			// Initializing the 'about box' OK Button
			instance.defaultButtons = {
				'OK': function() {
					this.close();
				}
			};
			
			// Initializing the 'about box' Lightbox
			instance.aboutBox = new Nokia.LightBox({
				element: '#lightbox-about',
				height: '80%',
				width: '80%',
				buttons: {
					"Close": function() {
						this.close();
					}
				}
			});

			// Initializing the GBC views
			
			instance.homeView = new Nokia.HomeView({
				container: '#home-view'
			});
			instance.resultsView = new Nokia.ResultsView({
				container: '#results-view'
			});
			instance.weatherView = new Nokia.WeatherView({
				container: '#weather-view'
			});
			instance.newsItemView = new Nokia.NewsItemView({
				container: '#news-item-view'
			});
			instance.picasaItemView = new Nokia.PicasaItemView({
				container: '#picasa-item-view'
			});
			instance.twitterItemView = new Nokia.TwitterItemView({
				container: '#twitter-item-view'
			});
			instance.wikipediaItemView = new Nokia.WikipediaItemView({
				container: '#wikipedia-item-view'
			});
			
			// Initializing the ViewManager
			
			instance.viewManager = new Nokia.ViewManager({
				beforeRender: function() {
					instance.showLoading();
				},
				render: function() {
					instance.hideLoading();
				}
			});

			instance.viewManager.add(instance.homeView);
			instance.viewManager.add(instance.resultsView);
			instance.viewManager.add(instance.weatherView);
			instance.viewManager.add(instance.twitterItemView);
			instance.viewManager.add(instance.picasaItemView);
			instance.viewManager.add(instance.newsItemView);
			instance.viewManager.add(instance.wikipediaItemView);

			var templateDefault = new Nokia.TemplateDefault({
				viewManager: instance.viewManager,
				showFirstView: false,
				header: {
					items: instance.getDefaultTopItems()
				}
			});
			
			// All is ready to show the first view!
			instance.viewManager.show(instance.homeView);
		},
		
		/*
		 * Method that initializes the top floating menu
		 * This method accepts an optional array of commands for the menu
		 */
		getFloatingMenuInstance: function(items) {
			var instance = this;
			
			if (instance.floatingMenu) {
				instance.floatingMenu.destroy();
			}

			// Build the two default floating menu commands
			var aboutCommand = {label: 'About', callback: function() { instance.aboutBox.open(); }};
			var exitCommand = {label: 'Exit', callback: function() { window.close(); }};
			
			if(!items)
				items = [aboutCommand, exitCommand];
			else
				items.push(aboutCommand, exitCommand);
			
			instance.floatingMenu = new Nokia.FloatingMenu({
				autoRender: false,
				element: '.nokia-template-header-icon-menu',
				elementHoverClass: 'nokia-template-header-icon-menu-hover',
				offsetTop: 60,
				items: items
			});
			
			return instance.floatingMenu;
		},
		
		/*
		 * The Default Template header can be configured with multiple items.
		 * GBC defines an header with these default items:
		 * 1) An exit button
		 * 2) A button to toggle the top floating menu
		 * 3) A title label
		 * 
		 * Each View can use and appropriately modify these items as needed
		 */
		getDefaultTopItems: function(floatingMenuRef) {
			var instance = this,
				floatingMenu = floatingMenuRef || instance.getFloatingMenuInstance();
			
			return [
				{classname: 'nokia-template-header-icon-close', callback: function() {window.close();}},
				{classname: 'nokia-template-header-icon-menu', callback: function() { floatingMenu.render().toggle(); } },
				{classname: 'nokia-template-header-icon-separator', navigable: false },
				{classname: 'nokia-template-header-title', label: 'GBC' },
				{classname: 'nokia-template-header-icon-separator-right', navigable: false }
			];
		}
	});
})();